package com.xunfang.demo.dao;

import com.xunfang.demo.entity.UserDemo;
import com.xunfang.demo.utils.DruidManger;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {

    QueryRunner qr = new QueryRunner(DruidManger.getDataSource());

    @Override
    public int insertUser(UserDemo userDemo) {
        String sql = " insert into userdemo values (null,?,?,?,?,?,?,?) ";
        Object [] params = {userDemo.getUserName(),userDemo.getUserPwd(),userDemo.getSex(),
            userDemo.getChk(),userDemo.getSel(),userDemo.getAddress(),userDemo.getBirthday()};
        try {
            return qr.update(sql, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public UserDemo selectBySome(String username, String userpwd) {
        String sql = " select * from userdemo where userName = ? and userPwd = ? ";
        try {
            return qr.query(sql,new BeanHandler<UserDemo>(UserDemo.class),username,userpwd);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public List<UserDemo> selectUserAll() {
        String sql = " select * from userdemo ";
        try {
            return qr.query(sql,new BeanListHandler<UserDemo>(UserDemo.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int delUserById(Integer id) {
        String sql = " delete from userdemo where id = ? ";
        try {
            return qr.update(sql,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
}
